package switchviz.graphic.gauge.bargraph;

import org.jdom.Element;

import switchviz.graphic.gauge.GaugePersistence;

public class BarGraphPersistence extends GaugePersistence
{
	private static final String HISTORYLENGTH = "historylength";
	private static final String CURRVALBAR = "currvalbarheight";
	private static final String LINEWIDTH = "linewidth";
	private static final String REPRESENTATION = "representation";
	private static final String DATAMAX = "datamax";
	private static final String DATAMIN = "datamin";
	private static final String DATADEPTH = "datadepth";
	
	
	public BarGraphPersistence(Element root)
	{
		_root = root;
	}
	
	public BarGraphDataModel getDataModel(Element root)
	{
		_root = root;
		
		return getDataModel();
	}
	
	public BarGraphDataModel getDataModel()
	{
		BarGraphDataModel datamodel = new BarGraphDataModel();
		super.setDataModel(datamodel);
		
		if(_root.getChild(DATAMAX) != null)
			datamodel.setDataMax(Float.valueOf(_root.getChildText(DATAMAX)));
		if(_root.getChild(DATAMIN) != null)
			datamodel.setDataMin(Float.valueOf(_root.getChildText(DATAMIN)));
		if(_root.getChild(DATADEPTH) != null)
			datamodel.setDataDepth(Float.valueOf(_root.getChildText(DATADEPTH)));
		if(_root.getChild(HISTORYLENGTH) != null)
			datamodel.setHistorylength(Integer.valueOf(_root.getChildText(HISTORYLENGTH)));
		if(_root.getChild(CURRVALBAR) != null)
			datamodel.setCurrvalbar(Float.valueOf(_root.getChildText(CURRVALBAR)));
		if(_root.getChild(LINEWIDTH) != null)
			datamodel.setLinewidth(Float.valueOf(_root.getChildText(LINEWIDTH)));
		if(_root.getChild(REPRESENTATION) != null)
			datamodel.setRepresentation(Integer.valueOf(_root.getChildText(REPRESENTATION)));
			
		return datamodel;
	}
	
	public Element getElement(BarGraphDataModel datamodel)
	{
		super.getElement(datamodel);
		addNode(DATAMAX, String.valueOf(datamodel.getDataMax()));
		addNode(DATAMIN, String.valueOf(datamodel.getDataMin()));
		addNode(DATADEPTH, String.valueOf(datamodel.getDataDepth()));
		addNode(HISTORYLENGTH, String.valueOf(datamodel.getHistorylength()));
		addNode(CURRVALBAR, String.valueOf(datamodel.getCurrvalbar()));
		addNode(LINEWIDTH, String.valueOf(datamodel.getLinewidth()));
		addNode(REPRESENTATION, String.valueOf(datamodel.getRepresentation()));
		return _root;
	}
}
